﻿using System.Net;
using System.Threading.RateLimiting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

namespace Extensions.Services.RateLimit.Policys;
/// <summary>
/// 并发算法
/// </summary>
public class ConcurrencyLimiterPolicy:BaseLimiterPolicy
{
    protected override RateLimitPartition<IPAddress> RateLimit(IPAddress ipAddress)
    {
        return RateLimitPartition.GetConcurrencyLimiter(ipAddress, _ => new ConcurrencyLimiterOptions()
        {
            // 每次允许的 数量
            PermitLimit = Config.PermitLimit,
            //队列处理顺序  
            QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
            // 等待队列的长度
            QueueLimit = Config.QueueLimit
        });
    }
}